<?php

class ParentFactory extends AbstractDecoratorFactory
{
    /**
     * @var AutoUpdater
     * #Mapper
     */
    private $_ModuleMapper;
    
    private $_selectedIDs = array();
    
    public function __construct(Factory $Factory, Mapper $ModuleMapper)
    {
        parent::__construct($Factory);
        $this->_ModuleMapper = $ModuleMapper;
    }
    
    /**
     * @return AutoUpdater
     * #Mapper
     */
    public function getModuleMapper()
    {
        return $this->_ModuleMapper;
    }
    
    protected function _select(Phlame $Phlame)
    {
        if (!in_array($Phlame->getID(), $this->_selectedIDs, true)) {
            $binder = constant(get_class($Phlame).'::MODULE_BINDER');
            $this->_selectedIDs = $Phlame->getMapper()->getIDs();
            $this->getModuleMapper()->selectByIDs($this->_selectedIDs, $binder);
        }
    }
    
    public function create(array $data)
    {
        $Phlame = parent::create($data);
        $this->_select($Phlame);
        $MM = $this->getModuleMapper(); 
        if (!($MM->getFactory() instanceof setOwner)) {
            throw new FactoryException('ModuleMapper\'s Factory doesn\'t implement setOwner');
        }
        $MM->getFactory()->setOwner(&$Phlame);
        $Ms = array();
        while ($M = $MM->get($id)) {
            $Ms[$id] = $M;
        }
        #$MM->getFactory()->setOwner();
        #var_dump($Phlame);
        $Phlame->addModules($Ms);
        return $Phlame;
    }
}
